package br.usp.pcs.acs.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.Cell;

public class EmployeeListPanel extends Composite{

	private HTML newerButtonHtml;
	private HTML countLabelHtml;
	private HTML olderButtonHtml;
	private HorizontalPanel navBarHorizontalPanel;
	private int startIndex, selectedRow = -1;
    private static final int VISIBLE_FLIGHT_COUNT = 10;
    private FlexTable flightListFlexTable;
    private Label id;
    private Label name;
    private Label cnpj;

	public EmployeeListPanel() {
		{
			VerticalPanel bottomVerticalPanel = new VerticalPanel();
			initWidget(bottomVerticalPanel);
			bottomVerticalPanel.setWidth("100%");
			{
				flightListFlexTable = new FlexTable();
				bottomVerticalPanel.add(flightListFlexTable);
				flightListFlexTable.setStyleName("flights-List");
				flightListFlexTable.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						// Select the row that was clicked (-1 to account for header row).
			            Cell cell = flightListFlexTable.getCellForEvent(event);
			            if (cell != null) {
			                int row = cell.getRowIndex();
			                if (row > 0) {
			                    selectRow(row - 1);
			                }
			            }
					}
				});
				flightListFlexTable.setWidth("100%");
				{
					id = new Label("ID");
					flightListFlexTable.setWidget(0, 0, id);
				}
				{
					name = new Label("Empregado");
					flightListFlexTable.setWidget(0, 1, name);
				}
				{
					cnpj = new Label("CPF");
					flightListFlexTable.setWidget(0, 2, cnpj);
				}
				{
					navBarHorizontalPanel = new HorizontalPanel();
					flightListFlexTable.setWidget(0, 4, navBarHorizontalPanel);
					navBarHorizontalPanel.setStyleName("flights-ListNavBar");
					flightListFlexTable.getRowFormatter().setStyleName(0, "flights-ListHeader");
					navBarHorizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
					navBarHorizontalPanel.setWidth("100%");
					{
						HorizontalPanel innerNavBarHorizontalPanel = new HorizontalPanel();
						navBarHorizontalPanel.add(innerNavBarHorizontalPanel);
						innerNavBarHorizontalPanel.setWidth("");
						{
							newerButtonHtml = new HTML("<a href='javascript:;'>&lt;Primeiros</a>", true);
							innerNavBarHorizontalPanel.add(newerButtonHtml);
							newerButtonHtml.setStyleName("gwt-HTML-navBar");
							newerButtonHtml.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {
									// Move back a page.
						            startIndex -= VISIBLE_FLIGHT_COUNT;
						            if (startIndex < 0) {
						                startIndex = 0;
						            }
						            else {
						                styleRow(selectedRow, false);
						                selectedRow = -1;
						                updateTable();
						            }
								}
							});
						}
						{
							countLabelHtml = new HTML("", true);
							innerNavBarHorizontalPanel.add(countLabelHtml);
							countLabelHtml.setStyleName("gwt-HTML-navBar");
						}
						{
							olderButtonHtml = new HTML("<a href='javascript:;'>\u00DAltimos&gt</a>", true);
							innerNavBarHorizontalPanel.add(olderButtonHtml);
							olderButtonHtml.setStyleName("gwt-HTML-navBar");
							olderButtonHtml.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {
									// Move forward a page.
						            startIndex += VISIBLE_FLIGHT_COUNT;
						            if (startIndex >= FlightInfoFakes.getFlightItemCount()) {
						                startIndex -= VISIBLE_FLIGHT_COUNT;
						            }
						            else {
						                styleRow(selectedRow, false);
						                selectedRow = -1;
						                updateTable();
						            }
								}
							});
						}
					}
				}
			}

		}
		initTable();
		updateTable();
		setWidth("100%");
	}
	
	private void initTable() {

		flightListFlexTable.getRowFormatter().setStyleName(0, "flights-ListHeader");

        // Initialize the rest of the rows.
        for (int i = 0; i < VISIBLE_FLIGHT_COUNT; ++i) {
        	flightListFlexTable.setText(i + 1, 0, "");
        	flightListFlexTable.setText(i + 1, 1, "");
            flightListFlexTable.setText(i + 1, 2, "");
            flightListFlexTable.getCellFormatter().setWordWrap(i + 1, 0, false);
            flightListFlexTable.getCellFormatter().setWordWrap(i + 1, 1, false);
            flightListFlexTable.getCellFormatter().setWordWrap(i + 1, 2, false);
            flightListFlexTable.getFlexCellFormatter().setColSpan(i + 1, 2, 1);
        }
    }
	
	/**
	* Selects the given row (relative to the current page).
	*
	* @param row the row to be selected
	*/
	private void selectRow(int row) {

	    // When a row (other than the first one, which is used as a header) is
	    // selected, display its associated MailItem.
		FlightInfo item = FlightInfoFakes.getFlightItem(startIndex + row);
	    if (item == null) {
	        return;
	    }

	    styleRow(selectedRow, false);
	    styleRow(row, true);

	    item.read = true;
	    selectedRow = row;
	}
	
	private void styleRow(int row, boolean selected) {
        if (row != -1) {
            if (selected) {
            	flightListFlexTable.getRowFormatter().addStyleName(row + 1, "flights-SelectedRow");
            }
            else {
            	flightListFlexTable.getRowFormatter().removeStyleName(row + 1, "flights-SelectedRow");
            }
        }
    }
	
	private void updateTable(){

        // Update the older/newer buttons & label.
        int count = FlightInfoFakes.getFlightItemCount();
        int max = startIndex + VISIBLE_FLIGHT_COUNT;
        if (max > count) {
            max = count;
        }

        newerButtonHtml.setVisible(startIndex != 0);
        olderButtonHtml.setVisible(startIndex + VISIBLE_FLIGHT_COUNT < count);
        countLabelHtml.setText("" + (startIndex + 1) + " - " + max + " of " + count);

        // Show the selected flight.
        int i = 0;
        for (; i < VISIBLE_FLIGHT_COUNT; ++i) {
            // Don't read past the end.
            if (startIndex + i >= FlightInfoFakes.getFlightItemCount()) {
                break;
            }

            FlightInfo item = FlightInfoFakes.getFlightItem(startIndex + i);

            // Add a new row to the table
            flightListFlexTable.setText(i + 1, 0, item.timeFlight);
            flightListFlexTable.setText(i + 1, 1, item.numberFlight);
            flightListFlexTable.setText(i + 1, 2, item.companyFlight);
            flightListFlexTable.setText(i + 1, 3, item.arrivingFlight);
        }

        // Clear any remaining slots.
        for (; i < VISIBLE_FLIGHT_COUNT; ++i) {
        	flightListFlexTable.setHTML(i + 1, 0, "&nbsp;");
        	flightListFlexTable.setHTML(i + 1, 1, "&nbsp;");
        	flightListFlexTable.setHTML(i + 1, 2, "&nbsp;");
        	flightListFlexTable.setHTML(i + 1, 3, "&nbsp;");
        }

        // Select the first row if none is selected.
        if (selectedRow == -1) {
            selectRow(0);
        }
        //throw new UnsupportedOperationException("Not yet implemented");
    }
}
